import api from '@/api/api'
import vm from '@/main'

const user = {
  state: {
    userInfo: JSON.parse(localStorage.getItem('template_userInfo')) || {},
    roles: JSON.parse(localStorage.getItem('template_roles')) || [],
    perms: JSON.parse(localStorage.getItem('template_perms')) || [],
    menus: JSON.parse(localStorage.getItem('template_menus')) || [],
    token: localStorage.getItem('template_token') || ''
  },
  mutations: {
    SETUSERINFO: (state, userInfo) => {
      state.userInfo = userInfo
      localStorage.setItem('template_userInfo', JSON.stringify(userInfo))
    },
    SETROLES: (state, roles) => {
      state.roles = roles
      localStorage.setItem('template_roles', JSON.stringify(roles))
    },
    SETPERMS: (state, perms) => {
      state.perms = perms
      localStorage.setItem('template_perms', JSON.stringify(perms))
    },
    SETMENUS: (state, menus) => {
      state.menus = menus
      localStorage.setItem('template_menus', JSON.stringify(menus))
    },
    SETTOKEN: (state, token) => {
      state.token = token
      localStorage.setItem('template_token', token)
    }
  },
  actions: {
    getUserInfo ({ commit }, params) {
      return new Promise((resolve, reject) => {
        api.getUserInfo(params).then(res => {
          if (!res.success || !res.data) {
            commit('SETUSERINFO', {})
            return res.success ? resolve(res) : reject(res)
          }
          commit('SETUSERINFO', res.data)
          resolve(res)
        }).catch(err => {
          window.console && console.log(err)
          reject(err)
        })
      })
    },
    getRoleAndPerm ({ commit }, params) {
      return new Promise((resolve, reject) => {
        api.getRoleAndPerm(params).then(res => {
          if (!res.success || !res.data) {
            commit('SETROLES', [])
            commit('SETPERMS', [])
            return res.success ? resolve(res) : reject(res)
          }
          commit('SETROLES', res.data.roles)
          commit('SETPERMS', res.data.permissions)
          resolve(res)
        }).catch(err => {
          window.console && console.log(err)
          reject(err)
        })
      })
    },
    getMenu ({ commit }, params) {
      return new Promise((resolve, reject) => {
        api.findMenuOfCurrUser(params).then(res => {
          if (!res.success || !res.data || !res.data.length) {
            commit('SETMENUS', [])
            return res.success ? resolve(res) : reject(res)
          }
          commit('SETMENUS', res.data)
          resolve(res)
        }).catch(err => {
          window.console && console.log(err)
          reject(err)
        })
      })
    },
    logout ({ commit, state }) {
      return new Promise((resolve, reject) => {
        api.logout(state.token).then(() => {
          commit('SETUSERINFO', {})
          commit('SETROLES', [])
          commit('SETPERMS', [])
          commit('SETMENUS', [])
          commit('SETTOKEN', '')
          commit('SET_CACHES', [])
          localStorage.clear()
          vm.$router.push({
            name: 'login',
            params: {
              logout: true
            }
          })
          resolve()
        }).catch(err => {
          vm.$notifyBox(vm.GLOBAL.MSG.CATCH_MSG)
          window.console && console.log(err)
          reject(err)
        })
      })
    }
  }
}

export default user
